package com.financing.wap.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.lang.StringUtils;
 
public class AddressUtil {
    /**
     * 获取URL返回的字符串
     * @param callurl
     * @param charset
     * @return
     */
    private static String callUrlByGet(String callurl,String charset){   
        String result = "";   
        try {   
            URL url = new URL(callurl);   
            URLConnection connection = url.openConnection();   
            connection.connect();   
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),charset));   
            String line;   
            while((line = reader.readLine())!= null){    
                result += line;   
                result += "\n";
            }
        } catch (Exception e) {   
            e.printStackTrace();   
            return "";
        }
        return result;
    }
    /**
     * 手机号码归属地
     * @param tel  手机号码
     * @return 135XXXXXXXX,联通/移动/电信,湖北武汉
     * @throws Exception
     * @author 
     */
    public static String getMobileLocation(String x,String y){
    	try {
    		if(StringUtils.isBlank(x) || StringUtils.isBlank(y)){
    			return "未知";
    		}
	            String url = "http://maps.google.cn/maps/api/geocode/json?language=CN&latlng=" + x +","+y;
	            String result = callUrlByGet(url,"UTF-8");
	            String address=StringUtil.fecth(result,"\"formatted_address\" : \"(.*?)\"",1);
                return address;
            
    	} catch (Exception e) {
			// TODO: handle exception
		}
    	return "未知";
    }
    
    
    public static void main(String[] args) {
            System.out.println(AddressUtil.getMobileLocation("31.219101","121.529099"));
    }

}